<?php
/**
 * Geneone: Content Management Framework
 * Taxonomy Set
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Object_Taxonomy_Set {
	/**
	 * Taxonomy Set ID
	 *
	 * @var integer
	 */
	var $setId;
	
	/**
	 * Taxonomy User ID
	 *
	 * @var mixed Integer for User ID, false otherwise
	 */
	var $userId = false;
	
	/**
	 * Class Constructor
	 *
	 * @var integer $objectId Object ID
	 * @var integer $setId Taxonomy Set ID
	 */
	function Gene_Object_Taxonomy_Set($setId, $userId=false) {
		$this->setId = $setId;
		
		$this->tableName = Gene::tableName("object_taxonomy");
		if ($userId) {
			$this->tableName = Gene::tableName("object_user_taxonomy");
			if ($userId != "*") {
				$this->userId = $userId;
			}
		}
	}
	
	/**
	 * Gets an array of popular labels in this set.
	 *
	 * @var integer $num Number of labels to return.
	 * @var string $order Order to return labels
	 *
	 * @return array Array of popular labels and numbers of objects with that
	 *               label. Ordered by number of objects.
	 */
	function getPopularLabels($num=10) {
		$db =& Gene::getDatabase();
		
		$db->setLimit($num);
		
		if ($this->userId) {
			$q = $db->prepare ("SELECT label, COUNT(object_id) AS objects FROM ".Gene::tableName("object_user_taxonomy")." WHERE user_id=? AND set_id=? GROUP BY label ORDER BY objects DESC");
			$res = $q->execute(array($this->userId, $this->setId));
		} else {
			$q = $db->prepare ("SELECT label, COUNT(object_id) AS objects FROM ".Gene::tableName("object_taxonomy")." WHERE set_id=? GROUP BY label ORDER BY objects DESC");
			$res = $q->execute(array($this->setId));
		}
		
		$popular = $res->fetchAll();
		
		$res->free();
		
		return $popular;
	}
	
	/**
	 * Gets all the labels in the set. Ordered alphabetically. 
	 *
	 * @return array Array of all labels in set and numbers of objects
	 *               with that label.
	 */
	function getLabels() {
		$db =& Gene::getDatabase();
		
		if ($this->userId) {
			$q = $db->prepare("SELECT label, COUNT(object_id) AS objects FROM ".Gene::tableName("object_user_taxonomy")." WHERE user_id=? AND set_id=? GROUP BY label ORDER BY label ASC");
			$res = $q->execute(array($this->userId, $this->setId));
		} else {
			$q = $db->prepare("SELECT label, COUNT(object_id) AS objects FROM ".Gene::tableName("object_taxonomy")." WHERE set_id=? GROUP BY label ORDER BY label ASC");
			$res = $q->execute(array($this->setId));
		}
		return $res->fetchAll();
	}
	
	/**
	 * Returns the number of objects with a specific label.
	 *
	 * @var string $label Label
	 *
	 * @return integer Number of objects with label.
	 */
	function getNumObjectsWithLabel($label) {
		$db =& Gene::getDatabase();
		
		if ($this->userId) {
			$q = $db->prepare ("SELECT COUNT(object_id) AS num FROM ".Gene::tableName("object_user_taxonomy")." WHERE user_id=? AND set_id=? AND label=?");
			$res = $q->execute(array($this->userId, $this->setId, $label));
		} else {
			$q = $db->prepare ("SELECT COUNT(object_id) AS num FROM ".Gene::tableName("object_taxonomy")." WHERE set_id=? AND label=?");
			$res = $q->execute(array($this->setId, $label));
		}
		
		return $res->fetchOne();
	}
	
	/**
	 * Returns objects with a label or labels specified.
	 *
	 * Intended to be compatible with Gene_Object_Model::getChilds(). Set per page, num, etc in
	 * Gene_Object object.
	 *
	 * @var string $label Label
	 * @var array $options List of options to modify the items returned or the
	 *                     order of items returned.
	 *                     * where string SQL WHERE clause
	 *                     * tables mixed Array listing of tables to join. If only
	 *                                    one, a string is accepted. Each of the
	 *                                    tables must have an object_id column.
	 *                     * order string SQL ORDER BY clause
	 *
	 * @return array List of objects with label.
	 */
	function getObjectsByLabel($label, $options=array()) {
		$object =& Gene_Object::get($this->setId);
		
		if ($this->userId) {
			$table = Gene::tableName("object_user_taxonomy");
			$where = Gene::tableName("object_user_taxonomy").".user_id='".$this->userId."' AND set_id='".$this->setId."' AND label='".$label."'";
		} else {
			$table = Gene::tableName("object_taxonomy");
			$where = "set_id='".$this->setId."' AND label='".$label."'";
		}
		
		if (isset($options['tables'])) {
			if (is_array($options['tables'])) {
				$options['tables'][] = $table;
			} else {
				$options['tables'] = array($options['tables'], $table);
			}
		} else {
			$options['tables'] = $table;
		}
		
		if (isset($options['where'])) {
			$options['where'] = "(".$options['where'].") AND ".$where;
		} else {
			$options['where'] = $where;
		}

		return $object->getChilds($options);
	}
}
?>